<?php
		
		$connection = mysql_connect('cmm.local','training','train@CMM') or die("Server not connected".mysql_error());
		$database = mysql_select_db('training') or die("database not selected".mysql_error());
		
		if(isset($_POST["submit"]))
			{
				$name=trim(addslashes(htmlspecialchars($_POST["Name"])));
				$age=trim(addslashes(htmlspecialchars($_POST["Age"])));
				$occupation=$_POST["Occupation"];
				$state=$_POST["status"];
				$address=trim(nl2br(addslashes(htmlspecialchars($_POST["Address"]))));

				
				

				$qry="insert into shine_employ (name,age,occupation,status,address) values('$name','$age','$occupation','$state','$address')";

				$res=mysql_query($qry,$connection);
				mysql_close($connection);
					
				header("Location:view.php");exit;
				//my simple comment

			}

?>

<html>
	<head>
	<title>form-post</title>
	<style type="text/css">
			tr{vertical-align:top;}
			#frm1{width:550px;}
			body{font-size:1.5em; margin:0px auto;}
	</style>
	<script type="text/javascript">
			var pat1=/[a-z]/;
			var pat2=/[0-9]/;
			

			function val()
			{
				error="";
				vals=true;

				if(document.frm1['Name'].value=="")
					{error+="Enter Name \n"; vals=false;}

				
				
				if(document.frm1['Age'].value=="")
					{error+="Enter Age \n"; vals=false;}
				
				
				if(document.frm1['Occupation'].selectedIndex==0)
					{error+="Select a Occupation \n"; vals=false;}

				if(document.frm1['Address'].value=="")
					{error+="Enter your address \n"; vals=false;}

				if(document.getElementsByName("status")[0].checked==false && document.getElementsByName("status")[1].checked==false)
				{
					error+="Select a state \n"; vals=false;
				}
				
				if(vals==false)
					{
						alert(error);
					}
				else
				{
					vals=true;
					error="";
				}
				return vals;
			}


			function dd()
			{
				document.getElementsByName("Name")[0].focus();
			}

			function onchangecheck(el)
			{
				switch(el.name)
				{
					case "Name":
					
						if(document.frm1['Name'].value!="" && !pat1.test(document.frm1['Name'].value))
						{
						alert("Enter text for name field !");
						document.frm1['Name'].style.color="red";
						document.frm1['Name'].focus();
						//document.frm1['Name'].style.backgroundColor="blue";
						}
						else
						{
						document.frm1['Name'].style.color="";
						}

						break;
					

					case "Age":
					
						if(document.frm1['Age'].value!="" && !pat2.test(document.frm1['Age'].value))
						{
						alert("Enter number for age field !");
						document.frm1['Age'].style.color="red";
						document.frm1['Age'].focus();
						}
						else
						{
						document.frm1['Age'].style.color="";
						}
						if((document.frm1['Age'].value>100))
						{
							alert("Age cant be greater than 100");
							document.frm1['Age'].value="";
							document.frm1['Age'].focus();
						}
						break;
					

					
				}

			}

	</script>
	</head>
	<body onload="dd();">
	<div align="center">
	<h2>Add a New Employee</h2>
		<form name="frm1" id="frm1" action="#" method="post" onsubmit="return val()">
			<fieldset>
				<legend>Personnal Details</legend>
				<table cellspacing="5" cellpadding="2" border=0>
				<tr><td>Name :</td><td><input type="text" name="Name" value="" maxlength="40" size="35" onblur="onchangecheck(this);"></td></tr>
				<tr><td>Age :</td><td><input type="text" name="Age" value="" maxlength="3" size="3" onblur="onchangecheck(this);"></td></tr>
				<tr>
				<td>Occupation :</td>
				<td>
				<select name="Occupation">
				<option name="emp" value="">--select--</option>
				<option name="doc" value="Doctor">Doctor</option>
				<option name="eng" value="Engineer">Engineer</option>
				<option name="teach" value="Teacher">Teacher</option>
				<option name="law" value="Lawyer">Lawyer</option>
				</select>
				<tr>
				<td>Status :</td>
				<td><input type="radio" name="status" value="1"/>Active
				<input type="radio" name="status" value="2"/>Inactive
				</td>
				</tr>
				<tr><td>Address :</td><td><textarea name="Address" rows="5" cols="45" ></textarea></td></tr>
				<tr>
				<td colspan="2" align="center"><input type="reset">&nbsp;<input type="submit" name="submit" value="submit"></td>
				</tr>
				</table>
			</fieldset>
		</form>
	</div>
		
	</body>
</html>

	
